/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.ieslaferreria.creditsintesis.rmi;

import edu.ieslaferreria.creditsintesis.logicgame.ActionStates;
import edu.ieslaferreria.creditsintesis.logicgame.ObjectWithStates;
import edu.ieslaferreria.creditsintesis.logicgame.actions.ConfigurationMapEvent;
import edu.ieslaferreria.creditsintesis.logicgame.actions.RmiEvent;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * clase que crea el puente del sevidor, crea la partida y manda la orden de
 * actualizar todos los cambios generados por los diferentes puentes a traves
 * de los eventos provocados por estos.
 * @author Administrador
 */
public class PontServerImpl  extends ServerImpl implements PontServer {

// modificar por un array de Eventos
    ActionStates actionStates;
    EventRemoteListener  listener;
    String name;


    public PontServerImpl(ActionStates actionStates, String name) throws RemoteException, MalformedURLException{
        super();
        this.actionStates=actionStates;
        this.name=name;

    }
    


    public void actualiza(Pont pont) throws RemoteException{
        Iterator<ObjectWithStates> iter;
        for(iter= this.actionStates.getAllObjects();iter.hasNext();){
            ObjectWithStates obNext = iter.next();
            /*Debug*/
            if(obNext==null){
//                System.out.println("Next �s null");
            }
            if(pont==null){
//                System.out.println("Pont �s null");
            }
            //System.out.println("actualizando puentes" + obNext.getName());
            pont.send(obNext.getConfigurationEvent());
            /*End debug*/
        }
        pont.send(this.actionStates.getEventMap());
    }

    public void send(RmiEvent Mis) throws RemoteException {
        this.listener.action(Mis);
    }

    public void setListener(EventRemoteListener listener) throws RemoteException {
        this.listener=listener;
    }

        public Pont createPartida(Pont pont, String alias,boolean flag) throws RemoteException {
        Partida par;

        par = new Partida(this);

        par.addClient(pont, alias,flag);

        this.partides.add(par);
        return new PontImpl(par.getListener(),"partida");
    }

    public boolean equals(Object obj) {
        try {
            Pont pont = (Pont) obj;
            return this.name.equals(pont.getPontName());
        } catch (RemoteException ex) {
            Logger.getLogger(PontServerImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public String getPontName() {
        return this.name;
    }

    public void setPontName(String pontName) throws RemoteException {
        this.name=pontName;
    }

    public int getPartidaCountClients(int numPartida) throws RemoteException {
      return this.partides.get(numPartida).countClients();
    }

    public List<String> getPartidaGetAlias(int numPartida) throws RemoteException {
       return this.partides.get(numPartida).getAlias();
    }

    public Pont getPartidaGetPontPlayer(int i, String get)throws RemoteException {
      return this.partides.get(i).getPontPlayer(get);
    }

    public int getPartidaGetPanelActivo(int i)throws RemoteException {
        return this.partides.get(i).getPanelActivo();
    }

    public void setPartidaGetPanelActivo(int i,int k)throws RemoteException {
       this.partides.get(i).setPanelActivo(k);
    }

  

}
